<?php

use yii\helpers\Url;

$web = Url::base();
$this->title = '特价活动';
?>


<script type="application/javascript">
    var institution;
    $(function () {
        var institution_data =  <?= json_encode($institutions) ?>;
        var hasModifiedInstitutionList = <?= json_encode($has_modified_institution_list) ?>;
        institution = $('#institution').treeAutocomplete({
            defaultText: '机构',
            source: institution_data,
            tagSource: hasModifiedInstitutionList,
            onSelected: function (element) {
            }
        });

        <?php
        if($institution_code != ''){
        ?>
        institution.setValue('<?=$institution_code?>');
        <?php
        }
        ?>

        $('#myModalEdit').modal({
            keyboard: false, backdrop: 'static', show: false
        });
        $('#myModalDetail').modal({
            keyboard: false, backdrop: 'static', show: false
        });
    });

    function filter() {
        var search_institution_code = institution.getValue();
        var name = $("#bargain_name").val();
        var start_datetime = $("#start_datetime").val();
        var end_datetime = $("#end_datetime").val();


        var href = window.location.href;
        var index = href.indexOf("?page=");
        if (index > 0) {
            href = href.substr(0, index);
        }
        var href_str = href + "?page=1";

        if (search_institution_code != '' && search_institution_code != null) {
            href_str = href_str + "&institution_code=" + search_institution_code;
        }

        if (name != '' && name != null) {
            href_str = href_str + "&name=" + name;
        }

        if (start_datetime != '' && start_datetime != null) {
            href_str = href_str + "&start_datetime=" + start_datetime;
        }

        if (end_datetime != '' && end_datetime != null) {
            href_str = href_str + "&end_datetime=" + end_datetime;
        }

        window.location.href = href_str;
    }

    function onAddData(id) {
        $("#myModalEdit").modal('show');
        var data = {};
        if (id != undefined) {
            data.id = id;
        }
        modalEdit(data);
    }

    //打开新增修改页面
    function modalEdit(data) {
        $("#modaledit").html('');
        $("#modalDetail").html('');
        loading.show();
        requestHtml('bargain-part-view', data, true, function (responseHtml) {
            $("#modaledit").html(responseHtml);
            loading.hide();
        });
    }

    function getDetail(id, institution_code) {
        $("#modaledit").html('');
        $("#modalDetail").html('');
        $("#myModalDetail").modal('show');
        var data = {};
        data.id = id;
        data.institution_code = institution_code;

        $("#modalDetail").html('');
        loading.show();
        requestHtml('bargain-part-detail', data, true, function (responseHtml) {
            $("#modalDetail").html(responseHtml);
            loading.hide();
        });
    }

    function saveData() {
        var id = $("#modaledit_submit_form").find("#id").val();
        var institution_code = to_institution.getValue();
        var name = $("#modaledit_submit_form").find("#name").val();
        var description = $("#modaledit_submit_form").find("#description").val();
        var start_datetime = $("#modaledit_submit_form").find("#start_datetime").val();
        var end_datetime = $("#modaledit_submit_form").find("#end_datetime").val();
        var is_enable = $("#modaledit_submit_form").find("#is_enabled").is(":checked");
        if (is_enable == true) {
            is_enable = 1;
        } else {
            is_enable = 0;
        }

        var goods = {};
        goods.list = [];
        var goodsChecks = $("#goods_table").find('input:checkbox[name=goods]:checked');
        for (var i = 0; i < goodsChecks.length; i++) {
            var g = {};
            g.code = $(goodsChecks[i]).val();
            g.old_price = $(goodsChecks[i]).parent().parent().find('[name="old_price"]').find("input").val();
            g.price = $(goodsChecks[i]).parent().parent().find('[name="price"]').html();
            var is_top = $(goodsChecks[i]).parent().parent().find('[name="is_top"]').find("input");
            if (is_top.is(":checked") == true) {
                g.is_top = 1;
            } else {
                g.is_top = 0;
            }
            goods.list.push(g);
        }

        if (institution_code == '' || institution_code == null) {
            $("#hint_message").text('机构不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        if (name == '' || name == null) {
            $("#hint_message").text('名称不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        if (start_datetime == '' || start_datetime == null) {
            $("#hint_message").text('开始时间不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        if (end_datetime == '' || end_datetime == null) {
            $("#hint_message").text('结束时间不能为空！');
            $("#hintMessageModal").modal('show');
            return false;
        }


        if (!compareDate(start_datetime, end_datetime)) {
            $("#hint_message").text('结束时间必须大于开始时间！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        var now = timestampToDatetime(new Date());
        if (!compareDate(now, end_datetime)) {
            $("#hint_message").text('结束时间必须大于当前时间！');
            $("#hintMessageModal").modal('show');
            return false;
        }


        if (goods.list.length == 0) {
            $("#hint_message").text('请选择特价商品！');
            $("#hintMessageModal").modal('show');
            return false;
        }

        var data = {};
        data.id = id;
        data.institution_code = institution_code;
        data.name = name;
        data.description = description;
        data.start_datetime = start_datetime;
        data.end_datetime = end_datetime;
        data.is_enable = is_enable;
        data.goodslist = goods.list;

        loading.show();
        requestJson("bargain-save-handler", data, true, function (response) {
            loading.hide();
            if (response.status == 0) {
                filter();
            } else {
                $("#hint_message").text(response.message);
                $("#hintMessageModal").modal('show');
            }
        });
    }

    function onDelData(id,institution_code) {
        var data = {};
        data.id = id;
        data.institution_code = institution_code;
        loading.show();
        requestJson("bargain-del-handler", data, true, function (response) {
            loading.hide();
            if (response.status == 0) {
                filter();
            } else {
                $("#hint_message").text(response.message);
                $("#hintMessageModal").modal('show');
            }
        });
    }

    //更新下级机构数据对话框
    function updateChilddata() {
        //取得选择的机构
        var type = 4;
        //取得选择的机构
        var institution_code = $("#institution").val();
        if (institution_code == null || institution_code == '') {
            alert("请选择机构！");
            return;
        }
        updateChildInstData("<?=Url::to(['institution/institution-childlist-part-view'])?>", institution_code, type);
    }

    //确定更新下级机构数据
    function confUpdateChildData() {
        var type = 4;
        confUpdateChildInstData("<?=Url::to(['institution/sys-under-institution-data'])?>", type);
    }
</script>
<style type="text/css">
    .modal-body {
        overflow: auto;
        max-height: 600px;
    }
</style>

<!-- head filter-->
<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group col-lg-2">
                <button class="btn btn-primary" type="button" onclick="onAddData()"><i class="fa fa-plus"></i> 新增
                </button>
                <?php if (\app\models\Permission::hasPermission("institution", "sys-under-institution-data")) {
                    ?>
                    <button class="btn btn-primary" type="button" onclick="updateChilddata()"><i class="fa fa-cog"></i>
                        更新下级机构数据
                    </button>
                    <?php
                } ?>
            </div>
            <div class="pull-right col-lg-10">
                <div class="col-lg-3">
                    <div id="institution"></div>
                </div>
                <div class="col-lg-2">
                    <input type="text" class="form-control" id="bargain_name" placeholder="活动名称" value="<?= $name ?>">
                </div>

                <div class="col-lg-2">
                    <div data-date="" class="input-group date form_date">
                        <input type="text" class="form-control" readonly="" size="16" id="start_datetime"
                               name="start_datetime" placeholder="开始时间"
                               value="<?= $start_datetime ?>">
                        <div class="input-group-btn">
                            <button type="button" class="btn btn-default date-reset"><i class="fa fa-times"></i>
                            </button>
                            <button type="button" class="btn btn-default date-set"><i class="fa fa-calendar"></i>
                            </button>
                        </div>
                    </div>
                </div>

                <div class="col-lg-2">
                    <div data-date="" class="input-group date form_date">
                        <input type="text" class="form-control" readonly="" size="16" id="end_datetime"
                               name="end_datetime" placeholder="结束时间"
                               value="<?= $end_datetime ?>">
                        <div class="input-group-btn">
                            <button type="button" class="btn btn-default date-reset"><i class="fa fa-times"></i>
                            </button>
                            <button type="button" class="btn btn-default date-set"><i class="fa fa-calendar"></i>
                            </button>
                        </div>
                    </div>
                </div>


                <div class="col-lg-2">
                    <button type="button" class="btn btn-primary" onclick="filter()"><i class="fa fa-search"></i> 搜索
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- body table-->
<div class="panel panel-default" id="card_list">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th style="width:20%">活动名称</th>
                    <th style="width:20%">机构</th>
                    <th style="width:20%">活动描述</th>
                    <th style="width:12%">开始时间</th>
                    <th style="width:12%">结束时间</th>
                    <th style="width:6%">启用</th>
                    <th style="width:10%">操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                if ($list != null && count($list) > 0) {
                    for ($i = 0; $i < count($list); $i++) {
                        ?>
                        <tr>
                            <td>
                                <button class="btn btn-link btn-xs" type="button"
                                        onclick="getDetail('<?= $list[$i]['id'] ?>','<?= $list[$i]['institution_code'] ?>')"
                                        data-toggle="modal"
                                        data-target="#myModalDetail">
                                    <?= $list[$i]['name'] ?>
                                </button>
                            </td>
                            <td>
                                <?= $list[$i]['institution_name'] ?>
                            </td>
                            <td>
                                <?= $list[$i]['description'] ?>
                            </td>
                            <td>
                                <?= $list[$i]['start_datetime'] ?>
                            </td>
                            <td>
                                <?= $list[$i]['end_datetime'] ?>
                            </td>


                            <td>
                                <?php
                                if ($list[$i]['is_enabled'] == 1) {
                                    echo ' <label class="label label-success">已启用</label>';
                                } else {
                                    echo ' <label class="label label-disable">未启用</label>';
                                }
                                ?>
                            </td>

                            <td>
                                <div class="btn-group">
                                    <button class="btn btn-link btn-xs" type="button"
                                            onclick="onAddData('<?= $list[$i]['id'] ?>')"
                                            data-toggle="modal"
                                            data-target="#myModalEdit">
                                        修改
                                    </button>
                                    <button class="btn btn-link btn-xs" type="button"
                                            onclick="onDelData('<?= $list[$i]['id'] ?>','<?= $list[$i]['institution_code'] ?>')">
                                        删除
                                    </button>

                                </div>
                            </td>
                        </tr>
                        <?php
                    }
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>

<!-- Modal card Edit-->
<div class="modal fade" id="myModalEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">特价活动</h4>
            </div>
            <div id="modaledit" class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="saveData()"><i class="fa fa-save"></i> 保存
                </button>
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>


<!-- Modal card detail-->
<div class="modal fade" id="myModalDetail" tabindex="-1" role="dialog" aria-labelledby="myModalDetailLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalDetailLabel">特价活动详情</h4>
            </div>
            <div id="modalDetail" class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>


<!-- card voucher-->
<div class="modal fade" id="myModalCardVoucher" tabindex="-1" role="dialog" aria-labelledby="myModalCardVoucherLable">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalCardVoucherLable">单证</h4>
            </div>
            <div id="modalCardVoucher" class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>


<!--删除确认对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="deleteModal" class="modal fade">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">是否确认操作？</h4>
                <input type="hidden" id="deleteDataId" class="form-control" value="">
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" onclick="delData()"><i class="fa fa-ok"></i> 确认</button>
                <button class="btn btn-primary" onclick="delClose()"><i class="fa fa-remove"></i> 取消</button>
            </div>
        </div>
    </div>
</div>

<!--提示对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="hintMessageModal" class="modal fade">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">提示</h4>
            </div>
            <div class="modal-body" id="hint_message" align="center">
            </div>
        </div>
    </div>
</div>

<!-- 下级机构选择列表-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="modalChildInstList" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <input type="hidden" id="updateChild_SuperInstCode" value="" readonly>
                <h4 class="modal-title">下级机构选择</h4>
            </div>
            <div id="childInstList" class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confUpdateChildData()"><i class="fa fa-save"></i>
                    确定
                </button>
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>

<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/datetimepicker-custom.css"/>
<script src="<?= $web ?>/js/bootstrap-datetimepicker.js"></script>
<script src="<?= $web ?>/js/pickers-init.js"></script>

